/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// Reg.cpp: implementation of the CReg class.
//
//////////////////////////////////////////////////////////////////////

#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include "Reg.h"

//#ifdef _DEBUG
//#undef THIS_FILE
//static char THIS_FILE[]=__FILE__;
//#define new DEBUG_NEW
//#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CReg::CReg()
{

}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

CReg::~CReg()
{

}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

void CReg::GenerateKey(int Type, int Seed, char* RetBuff)
{
	int RandNum = 0;
	int KeyNum = 0;
	int len = 0;
	int diff = 0;
	int i = 0;
	char RandBuff[5];
	char KeyBuff[7];

	srand((unsigned)time(NULL) + Seed);
	RandNum = rand();
	ltoa((long)RandNum, RandBuff, 36);
	len = strlen(RandBuff);
	diff = 4 - len;
	if(diff < 0) diff = 0;
	while(diff)
	{
		for(i = 2; i >= 0; i--)
		{
			RandBuff[i+1] = RandBuff[i];
		}
		RandBuff[0] = '0';		
		diff--;
	}
	RandBuff[4] = 0;

	KeyNum = RandNum * 2;
	KeyNum = KeyNum + Type;
	KeyNum = KeyNum * 32;
	KeyNum = KeyNum - 202;
	ltoa((long)KeyNum, KeyBuff, 36);
	len = strlen(KeyBuff);
	diff = 6 - len;
	if(diff < 0) diff = 0;

	while(diff)
	{
		for(i = 4; i >= 0; i--)
		{
			KeyBuff[i+1] = KeyBuff[i];
		}
		KeyBuff[0] = '0';		
		diff--;
	}
	KeyBuff[6] = 0;
	sprintf(RetBuff, "%c%c%c%c%c%c%c%c%c%c\0", KeyBuff[5], RandBuff[3], KeyBuff[1], RandBuff[2], KeyBuff[3], KeyBuff[0], RandBuff[1], KeyBuff[4], RandBuff[0], KeyBuff[2]);	
	strupr(RetBuff);
}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

int CReg::CheckKey(int Type, char* Key)
{
	int len = 0;
	int RandNum = 0;
	int KeyNum = 0;
	char RandBuff[5];
	char KeyBuff[7];
	len = strlen(Key);
	if(len != 10) return false;
	strlwr(Key);
	sscanf(Key, "%c%c%c%c%c%c%c%c%c%c", &KeyBuff[5], &RandBuff[3], &KeyBuff[1], &RandBuff[2], &KeyBuff[3], &KeyBuff[0], &RandBuff[1], &KeyBuff[4], &RandBuff[0], &KeyBuff[2]);
	strupr(Key);
	RandBuff[4] = 0;
	KeyBuff[6] = 0;

	RandNum = (int)strtoul(RandBuff, 0, 36);
	KeyNum = (int)strtoul(KeyBuff, 0, 36);

	KeyNum = KeyNum + 202;
	KeyNum = KeyNum / 32;
	KeyNum = KeyNum - Type;
	KeyNum = KeyNum / 2;

	if(KeyNum == RandNum)
	{
		return true;
	}
	return false;
}
